package com.hao.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.commonutils.R;
import com.hao.educms.entity.CrmBanner;
import com.hao.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author hao
 * @since 2020-12-15
 * 管理员页面
 */
@CrossOrigin
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService service;

    /**
     * 轮播图，分页查询
     *
     * @param page  页面
     * @param limit 限制
     * @return {@link R}
     */
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit) {
        Page<CrmBanner> bannerPage = new Page<>();
        service.page(bannerPage, null);
        return R.ok().data("items", bannerPage.getRecords())
                .data("total", bannerPage.getTotal());
    }

    @GetMapping("get/{id}")
    public R getBanner(@PathVariable String id) {
        CrmBanner banner = service.getById(id);
        return R.ok().data("item", banner);
    }

    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner banner) {
        service.save(banner);
        return R.ok();
    }

    @PutMapping("update")
    public R updateBanner(@RequestBody CrmBanner banner) {
        service.updateById(banner);
        return R.ok();
    }

    @DeleteMapping("remove/{id}")
    public R deleteBanner(@PathVariable String id) {
        service.removeById(id);
        return R.ok();
    }


}

